package app.Disthanitian.thaistyle.EmerMez;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;

public class ShowRiskZoneAll extends Activity {
	RiskZone a = new RiskZone();
	
	ListView listView;
	ArrayList  values;
	ArrayAdapter adapter;
	InputStream is = null;
	String js_result = "";
	HttpPost httppost = null;
	MemberUser user = new MemberUser();

	static UrlEmerMez URLlink = new UrlEmerMez();
	public static final String urlShowRiskZone = URLlink.getUrlshowrizkzone();
	public static String result = "";

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_riskzone);

		listView = (ListView) findViewById(R.id.About);

		WebPageTask task = new WebPageTask();
		task.execute();

	}

	public String ShowRiskZone() {

		//

		return postData(urlShowRiskZone);
	}

	public String postData(String urlShow) {

		HttpClient httpclient = new DefaultHttpClient();
		httppost = new HttpPost(urlShow);
		BufferedReader in = null;
		// StringBuffer sb = new StringBuffer("");

		try {
			// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));

			StringBuilder sb = new StringBuilder();

			String line;
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			js_result = sb.toString();
			return js_result;
		} catch (ClientProtocolException e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		} catch (IOException e) {
		}
		return null;
	}

	protected class WebPageTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {

			result = ShowRiskZone() ;
			return result;
		}

		protected void onPostExecute(String result) {
			 Toast.makeText(getApplicationContext(), "Show" + result,Toast.LENGTH_LONG).show();

			values = new ArrayList();

			try {

				JSONArray jArray = new JSONArray(result);

				for (int i = 0; i < jArray.length(); i++) {
					JSONObject jo = jArray.getJSONObject(i);

					int risk_id = jo.getInt("risk_id");
					int user_id = jo.getInt("user_id");
					String name = jo.getString("name");
					String oriLat = jo.getString("oriLat");
					String oriLng = jo.getString("oriLng");
					String destLat = jo.getString("destLat");
					String destLng = jo.getString("destLng");
					String type = jo.getString("type");
					String date_riskZone = jo.getString("date_riskZone");
					// a = new RiskZone();
					//a.set
					values.add(risk_id);
					values.add(user_id);
					values.add(name);
					values.add(oriLat);
					values.add(oriLng);
					values.add(destLat);
					values.add(user_id);
					//values.add("Name:" + name + "\n oriLat:" + oriLat+ "\n  oriLng:" +  oriLng    + "\n  destLat:" + destLat +"\ndestLng" +destLng);

					//adapter = new ArrayAdapter(ShowRiskZoneAll.this,android.R.layout.simple_list_item_1,android.R.id.text1, values);

					//listView.setAdapter(adapter);
					//adapter.notifyDataSetChanged();
				}

				// listView.setAdapter(adapter);
				// adapter.notifyDataSetChanged();
			} catch (JSONException e) {
				Log.e("log_tag", "Error parsing data " + e.toString());

			}

		}

	}
}
